<?php
/**
 * @Author: xuzhiyi
 * @Date:   2016-02-16 20:57:17
 * @Last Modified by:   xuzhiyi
 * @Last Modified time: 2016-02-28 01:14:13
 * ------------------------------------------------ *
 * 文章                                         *
 * ------------------------------------------------ */
namespace Admin\Model;

class ArticlesModel extends CommonModel{
    /* 新增
	 * ---------------------- */
	 public function addArticles(){
		$data = array();
		$data["catId"] = (int)I("catId");
		$data["articleTitle"] = I("articleTitle");
		$data["isShow"] = (int)I("isShow",0);
		$data["articleContent"] = I("articleContent");
		$data["articleKey"] = I("articleKey");
		$data["managerId"] = (int)session('admin.managerId');
		$data["createTime"] = date('Y-m-d H:i:s');
	    if($this->checkEmpty($data)){
			$m = M('articles');
			$rs = $m->add($data);
		    if(false !== $rs){
				return true;
			}
		}
		return false;
	 } 
     /* 修改
	  * ----------------------- */
	 public function editArticles(){
	 	$articleId = (int)I("articleId");
		$data = array();
		$data["catId"] = (int)I("catId");
		$data["articleTitle"] = I("articleTitle");
		$data["isShow"] = (int)I("isShow",0);
		$data["articleContent"] = I("articleContent");
		$data["articleKey"] = I("articleKey");
		$data["managerId"] = (int)session('admin.managerId');
	    if($this->checkEmpty($data)){	
			$m = M('articles');
		    $rs = $m->where("articleId=".$articleId)->save($data);
			if(false !== $rs){
				return true;
				
			}
		}
		return false;
	 } 
	 /* 获取指定文章
	  * --------------------------- */
     public function get(){
	 	$m = M('articles');
		return $m->where("articleId=".(int)I('articleId'))->find();
	 }
	 
	 /* 获取列表
	  * -------------------------- */
	  public function getArticles(){
	     $m = M('articles');
	     //$rs = $m->where('isShow=1')->order('articleId desc')->select();

	     $rs =$m->join('as a left join '.C("DB_PREFIX").'article_cats as b on a.catId=b.catId')
	     ->join('left join '.C("DB_PREFIX").'managers as c on a.managerId=c.managerId')
	     ->field('a.*,b.catName as parentName,c.loginName')->order('a.articleId desc')->select();


	     foreach($rs as $key=>$val){
	     	$rs[$key]['articleContent'] =htmlspecialchars_decode($val['articleContent'],ENT_COMPAT);
	     }
	     $output['data']=$rs;
		 return $output;
	  }
	  
	 /* 删除
	  * ------------------ */
	 public function delArticles(){
	    $m = M('articles');
	    $rs = $m->delete((int)I('articleId'));
		if(false !== $rs){
		   return true;
		}
		return false;
	 }
	 /* 改变状态
	 *--------------------------*/
	public function editStatus(){
	 	$a = M('articles');
	 	$data['isShow'] = (I('isShow')==1)?1:0;
	 	$rs = $a->where("articleId=".(int)I('articleId',0))->save($data);
	    if(false !== $rs){
			return true;
		}
	 	return false;
	}
}